package com.jdd966.navigation;

import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static ohos.agp.components.Image.ScaleMode.*;

/**
 * @author geyao
 * @date 2021/7/13
 * des:导航Tab
 */
public class NavigationTab extends DirectionalLayout {
    /**
     * Tab文本
     */
    private Text tabText;
    /**
     * Tab显示图标
     */
    private Image tabImage;
    /**
     * Tab文本文案
     */
    private String tabTextStr;
    /**
     * Tab文本大小
     */
    private int tabTextSize = 12;
    /**
     * Tab文本颜色/选中文本颜色
     */
    private Color tabTextColor, tabTextSelectedColor;
    /**
     * Tab图标资源id/选中图标资源id
     */
    private int tabImageResId, tabImageSelectedResId;
    /**
     * Tab 宽/高/图标宽/图标高
     */
    private int tabWidth = MATCH_CONTENT, tabHeight = MATCH_CONTENT,
            tabImageWidth = MATCH_CONTENT, tabImageHeight = MATCH_CONTENT;
    /**
     * 图片比例模式
     */
    private Image.ScaleMode tabImageScaleMode = ZOOM_CENTER;
    /**
     * 是否被选中
     */
    private boolean isSelected = false;
    /**
     * 文本与图标间的间隔
     */
    private int distance = 0;

    public NavigationTab(Context context) {
        this(context, null);
    }

    public NavigationTab(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public NavigationTab(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    /**
     * 设置Tab显示文本
     *
     * @param text 显示文本
     * @return NavigationTab
     */
    public NavigationTab setText(String text) {
        this.tabTextStr = text;
        return this;
    }

    /**
     * 设置Tab显示文本大小(默认12)
     *
     * @param size 文本大小
     * @return NavigationTab
     */
    public NavigationTab setTextSize(int size) {
        this.tabTextSize = size;
        return this;
    }

    /**
     * 设置Tab文本颜色
     *
     * @param color 文本颜色
     * @return NavigationTab
     */
    public NavigationTab setTextColor(Color color) {
        this.tabTextColor = color;
        return this;
    }

    /**
     * 设置Tab选中文本颜色
     *
     * @param color 文本颜色
     * @return NavigationTab
     */
    public NavigationTab setTextSelectedColor(Color color) {
        this.tabTextSelectedColor = color;
        return this;
    }

    /**
     * 设置Tab图标
     *
     * @param resId 图标资源id
     * @return NavigationTab
     */
    public NavigationTab setImage(int resId) {
        this.tabImageResId = resId;
        return this;
    }

    /**
     * 设置Tab选中图标
     *
     * @param resId 图标资源id
     * @return NavigationTab
     */
    public NavigationTab setSelectedImage(int resId) {
        this.tabImageSelectedResId = resId;
        return this;
    }

    /**
     * 设置图标大小(默认宽高为MATCH_CONTENT)
     *
     * @param width  图标宽度 {@link LayoutConfig}
     * @param height 图标高度 {@link LayoutConfig}
     * @return NavigationTab
     */
    public NavigationTab setImageSize(int width, int height) {
        this.tabImageWidth = width;
        this.tabImageHeight = height;
        return this;
    }

    /**
     * 设置图标比例模式(默认ZOOM_CENTER)
     *
     * @param scaleMode 比例模式 {@link ohos.agp.components.Image.ScaleMode}
     * @return NavigationTab
     */
    public NavigationTab setImageScaleMode(Image.ScaleMode scaleMode) {
        this.tabImageScaleMode = scaleMode;
        return this;
    }

    /**
     * 设置Tab大小(默认宽高为MATCH_CONTENT)
     *
     * @param width  Tab宽度 {@link LayoutConfig}
     * @param height Tab高度 {@link LayoutConfig}
     * @return NavigationTab
     */
    public NavigationTab setTabSize(int width, int height) {
        this.tabWidth = width;
        this.tabHeight = height;
        return this;
    }

    /**
     * 设置图标与文本间的间隔(默认间隔为0)
     *
     * @param distance 间隔
     * @return NavigationTab
     */
    public NavigationTab setDistance(int distance) {
        this.distance = distance;
        return this;
    }

    /**
     * 设置选中状态(默认为未选中false)
     *
     * @param selected 选中状态 true:选中 false:未选中
     * @return NavigationTab
     */
    public NavigationTab setTabSelected(boolean selected) {
        this.isSelected = selected;
        if (this.tabImage != null) {
            this.tabImage.setPixelMap(selected
                    ? (tabImageSelectedResId <= 0 ? tabImageResId : tabImageSelectedResId)
                    : tabImageResId);
        }
        if (this.tabText != null) {
            this.tabText.setTextColor(selected
                    ? (tabTextSelectedColor == null
                    ? (tabTextColor == null ? Color.BLACK : tabTextColor)
                    : tabTextSelectedColor)
                    : tabTextColor);
        }
        return this;
    }

    /**
     * 获取选中状态
     *
     * @return 选中状态
     */
    public boolean isSelected() {
        return this.isSelected;
    }

    /**
     * 设置控件权重
     *
     * @param weight 权重
     */
    public void setWeight(float weight) {
        DirectionalLayout.LayoutConfig layoutConfig = (LayoutConfig) getLayoutConfig();
        layoutConfig.weight = weight;
        setLayoutConfig(layoutConfig);
    }

    /**
     * 构建NavigationTab
     *
     * @param type 构建类型{@link BuildType}
     * @return NavigationTab
     */
    public NavigationTab build(BuildType type) {
        LayoutConfig config = new LayoutConfig();
        config.width = tabWidth;
        config.height = tabHeight;
        setLayoutConfig(config);
        setAlignment(LayoutAlignment.CENTER);
        switch (type) {
            case IMAGE_LEFT_TEXT_RIGHT:
                setOrientation(HORIZONTAL);
                buildImage();
                buildDistance();
                buildText();
                break;
            case TEXT_LEFT_IMAGE_RIGHT:
                setOrientation(HORIZONTAL);
                buildText();
                buildDistance();
                buildImage();
                break;
            case TEXT_TOP_IMAGE_BOTTOM:
                setOrientation(VERTICAL);
                buildText();
                buildDistance();
                buildImage();
                break;
            case IMAGE_TOP_TEXT_BOTTOM:
            default:
                setOrientation(VERTICAL);
                buildImage();
                buildDistance();
                buildText();
                break;
        }
        return this;
    }

    /**
     * 构建Image
     */
    private void buildImage() {
        if (this.tabImageResId <= 0) {
            return;
        }
        this.tabImage = new Image(getContext());
        this.tabImage.setPixelMap(isSelected
                ? (tabImageSelectedResId <= 0 ? tabImageResId : tabImageSelectedResId)
                : tabImageResId);
        this.tabImage.setWidth(tabImageWidth);
        this.tabImage.setHeight(tabImageHeight);
        this.tabImage.setScaleMode(tabImageScaleMode);
        addComponent(tabImage);
    }


    /**
     * 构建Text
     */
    private void buildText() {
        if (this.tabTextStr == null || this.tabTextStr.isEmpty()) {
            return;
        }
        this.tabText = new Text(getContext());
        this.tabText.setText(tabTextStr);
        this.tabText.setTextSize(tabTextSize);
        this.tabText.setTextColor(isSelected
                ? (tabTextSelectedColor == null
                ? (tabTextColor == null ? Color.BLACK : tabTextColor)
                : tabTextSelectedColor)
                : tabTextColor);
        addComponent(tabText);
    }

    /**
     * 构建间隔
     */
    private void buildDistance() {
        if (this.distance == 0) {
            return;
        }
        Component tabDistance = new Component(getContext());
        tabDistance.setWidth(distance);
        tabDistance.setHeight(distance);
        addComponent(tabDistance);
    }

    /**
     * 针对Tab内容排列
     */
    public enum BuildType {
        /**
         * 图标在上 文本在下
         */
        IMAGE_TOP_TEXT_BOTTOM,
        /**
         * 文本在上 图标在下
         */
        TEXT_TOP_IMAGE_BOTTOM,
        /**
         * 图标在左 文本在右
         */
        IMAGE_LEFT_TEXT_RIGHT,
        /**
         * 文本在左 图标在右
         */
        TEXT_LEFT_IMAGE_RIGHT
    }

    private void log(String str) {
        HiLogLabel hiLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x00303, NavigationTab.class.getSimpleName());
        HiLog.error(hiLogLabel, str);
    }

}
